package com.ghy.web.backend;

import com.ghy.entity.EbComment;
import com.ghy.entity.EbNews;
import com.ghy.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/guestbook")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page",defaultValue = "1")int psize,
                       @RequestParam(value = "pageSize",defaultValue = "5") int pageSize){
        System.out.println("start service.....;psize==>"+psize);

        List<EbComment> commentList = commentService.listByPage(psize,pageSize);

        int totalPage = commentService.getMaxPage(pageSize);

//        2.传递model（数据）和view视图(jsp)  key-value
        model.addAttribute("commentList",commentList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("curPage",psize);
        return "manage/guestbook";
    }

    @RequestMapping("/to_edit")
    public String toEdit(Model model,@RequestParam("ecId") int ecId){
        System.out.println("点击修改跳过来了，ecId="+ecId);
        //1.调用service层的方法查询点击的新闻的所有信息
        EbComment ebComment = commentService.selectOne(ecId);

        //2.指定model和jsp
        model.addAttribute("ebComment",ebComment);

        return "manage/guestbook-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbComment ebComment){
        System.out.println("ebComment==>"+ebComment);
        Integer result = commentService.updateNews(ebComment);
        if(result<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete/{ecId}")
    public String delete(@PathVariable("ecId") Integer ecId){
        //1.参数校验
        //2.执行删除（真实项目中 delete from 表名 where id=？）
        int result = commentService.remove(ecId);
        if(result==0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
}
